<template>
    <div v-if="visible" class="blm-source-detail" @click.self="close">
        <div class="blm-source-detail-container">
            <div class="blm-source-detail-header">
                <div class="blm-source-detail-title"></div>
                <div class="blm-source-detail-close">
                    <img
                        style="width: 18px"
                        :src="require('@/assets/images/close.png')"
                        alt=""
                        @click.stop="close"
                    />
                </div>
            </div>
            <div class="blm-source-detail-content">
                <div v-if="xuanguDetail" class="scroll-more-container">
                    <jgy-render-sdk
                        statName="yyzs"
                        :answer="xuanguDetail"
                        sourceType="Iwencai"
                        language="zh"
                        :jumpFun="jgyJumpFun"
                    />
                    <div
                        @click="loadMoreData"
                        class="load-next-btn"
                        v-if="showMoreBtn"
                        :class="{ 'load-next-disable': showMoreLoading }"
                    >
                        加载更多
                    </div>
                </div>
                <div v-else class="blm-source-detail-loading">加载中...</div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        xuanguData: {
            type: Object | Array,
            default() {
                return {};
            }
        },
        visible: {
            type: Boolean,
            default: false
        },
        total: {
            type: Number,
            defaul: 0
        }
    },
    data() {
        return {
            xuanguDetail: null,
            currentPage: 1,
            showMoreBtn: false,
            showMoreLoading: false
        };
    },
    created() {},
    watch: {
        visible(n) {
            if (n) {
                this.loadData(this.xuanguData);
            } else {
                this.xuanguDetail = null;
            }
        }
    },
    methods: {
 
        loadMoreData() {
            if (this.showMoreLoading) {
                return;
            }
            this.currentPage+=1;
            this.loadData(this.xuanguData)
        },
        loadData(dataExtra) {
            let formData = {
                data_ext_params: dataExtra.data_ext_params,
                page: this.currentPage,
                perpage: dataExtra.per_page || 30,
                query_type: dataExtra.query_type,
                source: 'b_zhengquan_changjiang',
                short_condition: "",
                token: dataExtra.token,
                query: dataExtra.query
            };
            if (dataExtra.model_sql) {
                formData.model_sql = dataExtra.model_sql;
            } else {
                formData.condition = dataExtra.condition;
            }

            this.fetchRenderData(formData);
        },
      
        fetchRenderData(formData) {
            const url = "./ai/moreXuangu/gateway/render/getData";
            this.showMoreLoading = true;
            fetch(url, {
                method: "POST",
                body: JSON.stringify(formData),
                headers: {
                    "Content-Type": "application/json"
                }
            })
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    // 这个接口偶尔会出现异常状态
                    if (data.error_code === -9999) {
                        this.fetchData(formData);
                        return;
                    }
                    this.showMoreLoading = false;

                    if (data.answer.components[0]) {
                        window.chatSseSdk.handleTableData(data.answer,{filterRateCols:true,
                            filterCustomCols:(col)=>{//自定义过滤
                                        return col.key.indexOf('费用')===-1;
                                }

                        })
                        if (!this.xuanguDetail) {
                            this.xuanguDetail = data.answer;
                        } else {
                            this.xuanguDetail.components[0].data.datas =
                                this.xuanguDetail.components[0].data.datas.concat(
                                    data.answer.components[0].data.datas
                                );
                        }
                        if (
                            this.xuanguDetail.components[0].data.datas.length >=
                            this.total
                        ) {
                            this.showMoreBtn = false;
                        } else {
                            this.showMoreBtn = true;
                        }
                    }
                })
                .catch(error => console.error("Error:", error));
        },
        close() {
            this.currentPage = 1;
            this.$emit("update:visible", false);
        },
        jgyJumpFun(v) {
            console.log(v);
        }
    }
};
</script>
<style scoped lang="less">
@import (once) '@/styles/variable.less';
.blm-source-detail {
    width: 100%;
    position: fixed;
    inset: 0;
    background: @black-color-a-45;
    z-index: 123;
}
.blm-source-detail-container {
    background: @white-color-4;
    position: absolute;
    bottom: 0;
    padding: 12px;
    width: 100%;
    box-sizing: border-box;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
}
.blm-source-detail-content {
    background: @white-color-1;
    padding: 6px;
}
.blm-source-detail-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 12px;
}
.blm-source-detail-content /deep/ .chunk-title {
    display: none;
}
.blm-source-detail-content-title {
    margin-bottom: -12px;
}
.blm-source-detail-loading {
    height: 54px;
    line-height: 54px;
    text-align: center;
    color: @black-color-13;
}
.blm-source-detail-url-title {
    color: @black-color-9;
}
.blm-source-detail-url-value {
    color: @black-color-13;
    font-size: 12px;
    white-space: nowrap; /* 防止文本换行 */
    overflow: hidden; /* 隐藏溢出的文本 */
    text-overflow: ellipsis;
}
.blm-source-detail-url-item {
    background: @white-color-1;
    padding: 12px;
    margin-bottom: 8px;
    border-radius: 4xp;
}
.blm-source-detail-content,
.blm-source-detail-url {
    max-height: calc(100vh - 100px);
    overflow-y: auto;
}
.load-next-btn {
    text-align: center;
    color: @primary-1;
    font-size: 12px;
}
.load-next-disable {
    color: @white-color-10;
}
</style>
